
#ifndef RTCONNECORTHREADPROXY_H
#define RTCONNECORTHREADPROXY_H

#include "RtConnectionInterface.h"
#include "RtThreadManager.h"
#include "RtInetAddr.h"
#include "RtTimeValue.h"
#include "RtUtilClasses.h"
#include "RtAcceptorConnectorSinkThreadProxy.h"

class /*RT_API_EXPORT*/ CRtConnectorThreadProxy 
	: public IRtConnector
	, public CRtAcceptorConnectorSinkThreadProxyT<CRtConnectorThreadProxy>
	, public CRtReferenceControlMutilThread
	, public CRtStopFlag
{
public:
	CRtConnectorThreadProxy(
		CRtConnectionManager::CType aType,
		CRtThread *aThreadNetwork = NULL,
		CRtThread *aThreadUser = NULL);
	
	virtual ~CRtConnectorThreadProxy();

	// interface IRtReferenceControl
	virtual DWORD AddReference();
	virtual DWORD ReleaseReference();
	virtual void OnReferenceDestory();

	// interface IRtAcceptorConnectorId
	virtual BOOL IsConnector();

	// interface IRtConnector
	virtual void AsycConnect(
		IRtAcceptorConnectorSink *aSink,
		const CRtInetAddr &aAddrPeer,
		CRtTimeValue *aTimeout = NULL,
		CRtInetAddr *aAddrLocal = NULL);

	virtual void CancelConnect();

	IRtAcceptorConnectorId* GetActualAcceptorConnectorId()
	{
		return m_pConActual.Get();
	}

	// we have to overlaod this function we want to ResetSink.
	void SetStopFlag();

private:
	CRtThread *m_pThreadUser;
	CRtThread *m_pThreadNetwork;
	CRtAutoPtr<IRtConnector> m_pConActual;
	CRtConnectionManager::CType m_Type;

	friend class CEventAsycConnect;
	friend class CEventCancelConnect;
	friend class CRtAcceptorConnectorSinkThreadProxyT<CRtConnectorThreadProxy>;
	friend class CEventOnConnectIndication<CRtConnectorThreadProxy>;
};

	class CEventAsycConnect : public IRtEvent
	{
	public:
		CEventAsycConnect(
			CRtConnectorThreadProxy *aConnectorThreadProxy,
			IRtAcceptorConnectorSink *aSink, 
			const CRtInetAddr &aAddrPeer, 
			CRtTimeValue *aTimeout,
			CRtInetAddr *aAddrLocal);

		virtual ~CEventAsycConnect();

		virtual RtResult OnEventFire();

	private:
		CRtAutoPtr<CRtConnectorThreadProxy> m_pOwnerThreadProxy;
		IRtAcceptorConnectorSink *m_pSink;
		CRtInetAddr m_addrPeer;
		CRtTimeValue m_tvTimeout;
		CRtTimeValue *m_pParaTimeout;
		CRtInetAddr *m_pParaAddrLocal;
		CRtInetAddr m_addrLocal;
	};

	class CEventCancelConnect : public IRtEvent
	{
	public:
		CEventCancelConnect(CRtConnectorThreadProxy *aConnectorThreadProxy);

		virtual ~CEventCancelConnect();

		virtual RtResult OnEventFire();

	private:
		CRtAutoPtr<CRtConnectorThreadProxy> m_pOwnerThreadProxy;
	};

#endif // !RTCONNECORTHREADPROXY_H
